/**
 * Created by xuweijie on 2018/1/14.
 */
layui.use(['element', 'layer', 'form'], function () {
    var element = layui.element;
    $(function () {
        $('.updateMenuBtn').click(function () {
            var myClass = $(this).parents('.layui-colla-title').next('.layui-colla-content');
            var classContent = myClass.attr('class');
            if (classContent.indexOf('layui-show') > 0)
                myClass.removeClass('layui-show');
            else
                myClass.addClass('layui-show');
            var menuId = $(this).attr('menuId');
            var menuName = $(this).attr('menuName');
            var menuUrl = $(this).attr('menuUrl');
            var rootId = $(this).attr('rootId');
            updateMenuInfo(menuId, menuName, menuUrl, rootId, 0);
        });

        $('.deleteMenuBtn').click(function () {
            var myClass = $(this).parents('.layui-colla-title').next('.layui-colla-content');
            var classContent = myClass.attr('class');
            if (classContent.indexOf('layui-show') > 0)
                myClass.removeClass('layui-show');
            else
                myClass.addClass('layui-show');
            var menuId = $(this).attr('menuId');
            deleteMenuInfo(menuId);
        });

        $('.updateItemMenu').click(function () {
            var menuId = $(this).attr('menuId');
            var menuName = $(this).attr('menuName');
            var menuUrl = $(this).attr('menuUrl');
            var rootId = $(this).attr('rootId');
            var pid = $(this).attr('parentId');
            updateMenuInfo(menuId, menuName, menuUrl, rootId, pid);
        });

        $('.deleteItemMenu').click(function () {
            var menuId = $(this).attr('menuId');
            deleteMenuInfo(menuId);
        });

        //删除的方法
        function deleteMenuInfo(menuId) {
            layui.use(['layer', 'form'], function () {
                var layer = layui.layer;
                var form = layui.form;
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 2,
                    resize:true,
                    content:'<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
                    '(涉及菜单,请慎重)是否需要删除?删除后将不可恢复</div>',
                    area:  '450px',
                    shade: 0.8,
                    shadeClose:true,
                    anim:1,
                    id: 'LAY_layuipro5', //设定一个id，防止重复弹出
                    btn: ['绝不后悔', '我怂了'],
                    moveType: 1,
                    yes:function (index, layero) {
                        var params=$.param({
                            menuId:menuId
                        },true);
                        $.ajax({
                            url:'/admin/menu/deleteMenu',
                            type:'post',
                            data:params,
                            dataType:'json',
                            success:function (e) {
                                layer.msg(e.msg);
                                layer.close(index);
                                if (e.msg == "删除成功")
                                    setTimeout(function () {
                                        window.location.href = "/admin/index";
                                        window.location.href = "/admin/menu/menulist";
                                    },2000);
                            }
                        })
                    }
                })
            });

        }


        //更新的方法
        function updateMenuInfo(menuId, menuName, menuUrl, rootId, parentMenu) {
            var content = $('#updateMenuView');
            layui.use(['layer', 'form'], function () {
                var layer = layui.layer;
                var form = layui.form;
                layer.open({
                    type: 1,
                    skin: 'layui-layer-molv',
                    title: ['更新菜单', 'font-size:18px;'],
                    closeBtn: 1,
                    shade: 0.3,
                    content: content,
                    area: ['50%', '50%'],
                    shadeClose: true,
                    resize: true,
                    anim: 1,
                    id: 'LAY_layuirole13', //设定一个id，防止重复弹出
                    moveType: 1,
                    maxmin: false,
                    success: function (layero, index) {
                        $('#menuName').val(menuName);
                        $('#menuUrl').val(menuUrl);
                        $('#myroot').val(rootId);
                        if (parentMenu != 0) {
                            $('#parentMenu').parents('.layui-form-item').show();
                            $('#parentMenu').val(parentMenu);
                        }
                        form.render();
                        $('#updateMenuBtn').click(function () {
                            var name = $('#menuName').val();
                            var url = $('#menuUrl').val();
                            var newrootId = $('#myroot').val();
                            var pid = parentMenu;
                            if (parentMenu != 0)
                                pid = $('#parentMenu').val();
                            if (name == menuName && url == menuUrl && newrootId == rootId && pid == parentMenu) {
                                layer.msg("未做任何修改");
                            } else if (name == '' || url == '') {
                                layer.msg("请先完善信息");
                            } else {
                                var params = $.param({
                                    id: menuId,
                                    menu: name,
                                    url: url,
                                    rootId: newrootId,
                                    pid: pid
                                }, true);
                                $.ajax({
                                    url: '/admin/menu/updateMenu',
                                    type: 'post',
                                    data: params,
                                    dataType: 'json',
                                    success: function (e) {
                                        layer.msg(e.msg);
                                        if (e.msg == "更新成功") {
                                            setTimeout(function () {
                                                window.location.href = "/admin/index";
                                                window.location.href = "/admin/menu/menulist"
                                            }, 2000);
                                        }
                                    }
                                });
                            }
                        });
                        $('#resetMenuBtn').click(function () {
                            $('#menuName').val('');
                            $('#menuUrl').val('');
                            $('#myroot').val(0);
                            form.render();
                        });
                    },
                    cancel: function (index, layero) {
                        $('#menuName').val('');
                        $('#menuUrl').val('');
                        $('#myroot').val(0);
                        $('#parentMenu').val(0);
                        form.render();
                    }
                })
            });
        }

        $('#resetMsg').click(function () {
            var form =layui.form;
            $('#newMenuName').val('');
            $('#newMenuUrl').val('');
            $('#newParentMenu').val(0);
            $('#newMyroot').val(0);
            form.render();
        });

        $('#addMenuBtn').click(function () {
            var layer =layui.layer;
            var name = $('#newMenuName').val();
            var url = $('#newMenuUrl').val();
            var pid = $('#newParentMenu').val();
            var newrootId = $('#newMyroot').val();
            if (name == '')
                layer.msg("请输入菜单名");
            if (url == '')
                url = "#";
            var params = $.param({
                menu: name,
                url: url,
                rootId: newrootId,
                pid: pid
            }, true);
            $.ajax({
                url: '/admin/menu/addMenu',
                type: 'post',
                data: params,
                dataType: 'json',
                success: function (e) {
                    layer.msg(e.msg);
                    if (e.msg == "添加成功") {
                        setTimeout(function () {
                            window.location.href = "/admin/index";
                            window.location.href = "/admin/menu/menulist"
                        }, 2000);
                    }
                }
            });
        })

    });
});

